#!/bin/sh
set -e

cur_path=$(readlink -f $(dirname $0))
PLATFOROM=$(uname -m) 

if [ -z "$1" ]; then
    echo "Usage: generate_front_rpm.sh version"
    exit -1 
fi 

yum install -y rpmdevtools nodejs npm
rpmdev-setuptree
cp $cur_path/pentastor-frontend.spec ~/rpmbuild/SPECS/
sed -i "s/version 2.1/version $1/g" ~/rpmbuild/SPECS/pentastor-frontend.spec
rm -f ~/rpmbuild/SOURCES/pentastor-frontend-$1.tar.gz
if [ ! -d $cur_path/../seabed-frontend ]; then
  mkdir -p $cur_path/../seabed-frontend
  cp -r $cur_path/* $cur_path/../seabed-frontend
fi
tar -zcvf ~/rpmbuild/SOURCES/pentastor-frontend-$1.tar.gz  $cur_path/../seabed-frontend/

rpmbuild -bb ~/rpmbuild/SPECS/pentastor-frontend.spec
mkdir -p $cur_path/output
rm -f $cur_path/output/pentastor-frontend-$1*$PLATFOROM.rpm
mv -f ~/rpmbuild/RPMS/$PLATFOROM/pentastor-frontend-$1*$PLATFOROM.rpm $cur_path/output/
realpath $cur_path/output/pentastor-frontend-$1*$PLATFOROM.rpm

